﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace QLTV_GiaoDien
{
    public partial class frm_DocGia_Lop_Them : DevExpress.XtraEditors.XtraForm
    {
        public frm_DocGia_Lop_Them()
        {
            InitializeComponent();
        }
        private int _flag;

        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }
        private int _MaLop;

        public int MaLop
        {
            get { return _MaLop; }
            set { _MaLop = value; }
        }
        private string _TenLop;

        public string TenLop
        {
            get { return _TenLop; }
            set { _TenLop = value; }
        }
        private string _TenDonVi;

        public string TenDonVi
        {
            get { return _TenDonVi; }
            set { _TenDonVi = value; }
        }
        private string _KhoaHoc;

        public string KhoaHoc
        {
            get { return _KhoaHoc; }
            set { _KhoaHoc = value; }
        }
        private string _GhiChu;

        public string GhiChu
        {
            get { return _GhiChu; }
            set { _GhiChu = value; }
        }
        public void GetData()
        {
            txt_MaLop.Text = MaLop.ToString();
            txt_TenLop.Text = TenLop;
            cob_TenDonVi.Text = TenDonVi;
            txt_KhoaHoc.Text = KhoaHoc;
            txt_GhiChu.Text = GhiChu;
            //
            DataTable dt = new DataTable();
            string sql = "select * from tbl_DonVi,tbl_Lop where tbl_DonVi.MaDonVi=tbl_Lop.MaDonVi and tbl_Lop.MaLop='"+txt_MaLop.Text+"'";
            dt = lop_bll.taoBang(sql);
            cob_MaDonVi.DataSource = dt;
            cob_MaDonVi.DisplayMember = "MaDonVi";
            cob_TenDonVi.DataSource = dt;
            cob_TenDonVi.DisplayMember = "TenDonVi";
        }
        public delegate void GetInt(int kt);
        public GetInt GetLoad;
        public void LoadForm()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Lớp";
                txt_MaLop.Enabled = false;
            }
            if (Flag == 1)
            {
                this.Text = "Sửa Lớp";
                GetData();
            }
        }
        Lop_BLL lop_bll = new Lop_BLL();
        private void frm_DocGia_Lop_Them_Load(object sender, EventArgs e)
        {
            LoadForm();
            cob_MaDonVi.Hide();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void btn_Luu_Click(object sender, EventArgs e)
        {
            Lop_Public l = new Lop_Public();
            if (txt_TenLop.Text != "" && txt_KhoaHoc.Text != "" && cob_TenDonVi.Text != "")
            {
                if (Flag == 0)
                {
                    //them
                    l.TenLop = txt_TenLop.Text;
                    l.MaDonVi = int.Parse(cob_MaDonVi.Text);
                    l.KhoaHoc = txt_KhoaHoc.Text;
                    l.GhiChu = txt_GhiChu.Text;
                    lop_bll.Insert_Lop(l, l, l, l);
                    //
                    string d = DateTime.Now.ToString();
                    nk.MaNV = tg.MaNV;
                    nk.NgayGio = d;
                    nk.HanhDong = "Thêm";
                    nk.MoTa = "Thêm mới thông tin Lớp: '" + txt_TenLop.Text + "'";
                    nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                    //
                    XtraMessageBox.Show("Thêm mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Huy_Text(this);
                }
                if (Flag == 1)
                {
                    if (txt_MaLop.Text != "")
                    {
                        //sua
                        l.MaLop = int.Parse(txt_MaLop.Text);
                        l.TenLop = txt_TenLop.Text;
                        l.MaDonVi = int.Parse(cob_MaDonVi.Text);
                        l.KhoaHoc = txt_KhoaHoc.Text;
                        l.GhiChu = txt_GhiChu.Text;
                        lop_bll.Update_Lop(l, l, l, l, l);
                        //
                        string d = DateTime.Now.ToString();
                        nk.MaNV = tg.MaNV;
                        nk.NgayGio = d;
                        nk.HanhDong = "Sửa";
                        nk.MoTa = "Sửa thông tin Lớp: '" + TenLop.ToString() + "' thành: '" + txt_TenLop.Text + "'";
                        nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                        //
                        XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Huy_Text(this);
                    }
                    else
                        XtraMessageBox.Show("Bạn chưa nhập Mã lớp cần sửa!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                
            }
            else
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            GetLoad(1);
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
                ctr.Text = string.Empty;
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        private void btn_Huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cob_TenDonVi_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql = "select * from tbl_DonVi";
            dt = lop_bll.taoBang(sql);
            cob_MaDonVi.DataSource = dt;
            cob_MaDonVi.DisplayMember = "MaDonVi";
            cob_TenDonVi.DataSource = dt;
            cob_TenDonVi.DisplayMember = "TenDonVi";
        }

        private void txt_MaLop_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }


    }
}